package com.controller;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.model.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by chaiyuan on 2017/5/3.
 */
public class StudentController extends Controller {

    public void index() {
        render("/index_student.jsp");
    }
    public void view(){renderJsp(getPara("page"));}

    /**
     * 选题
     */
    public void select() {
        Student stu = getSessionAttr("user");
        int project_id = getParaToInt("selected");
        int stu_id = stu.getInt("stu_id");
        new UserProject().set("student_id",stu_id).set("project_id",project_id).save();
        redirect("/student");

    }

    /**
     * 选题结果
     */
    public void result(){
        Student stu = getSessionAttr("user");
        UserProject userProject = UserProject.dao.findById(stu.getInt("stu_id"));
        Project project = userProject.getProject();
        Teacher teacher = project.getTeacher();
        setAttr("project",project);
        setAttr("teacher",teacher);
        String status = null;
        switch (project.getInt("status")){
            case 0:
                status="未确认";
                break;
            case 1:
                status="已确认";
                break;
            case 2:
                status="已拒绝，等待随机分配题目";
                default:
                    break;

        }
        setAttr("status",status);
        render("select_result.jsp");
    }

    /**
     * 成绩查询
     */
    public void grade(){
        Project project = ((Student)getSessionAttr("user")).getProject();
        setAttr("project",project);
        setAttr("teacher",project.getTeacher());
        render("grade.jsp");

    }
    /**
     *上传论文
     */
    public void upload(){
        UploadFile file = getFile("page");
        String saveName = String.valueOf(System.currentTimeMillis());
        File sourse = file.getFile();
        String RealName = file.getFileName();
        try{
            FileInputStream fis = new FileInputStream(sourse);
            File target = new File(PathKit.getWebRootPath()+"/upload",saveName+".doc");
            FileOutputStream fos = new FileOutputStream(target);
            byte[] bts = new byte[300];
            while (fis.read(bts,0,300)!=-1){
                fos.write(bts,0,300);
            }
            fos.close();
            fis.close();
            sourse.delete();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        Student stu = getSessionAttr("user");
        Student.dao.findById(stu.getInt("stu_id")).set("page",saveName+".doc").set("page_name",RealName).update();
        setSessionAttr("user",stu);
        redirect("/student");
    }


}
